package xing.service.controller;

import com.querydsl.core.group.Group;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xing.application.service.page.ModuleService;
import xing.application.service.page.dto.ModuleSort;
import xing.application.service.page.dto.ReplaceModuleInput;
import xing.domain.page.PageModule;
import xing.domain.page.PageModuleItem;
import xing.domain.page.annotation.Access;
import xing.domain.page.dto.ModuleList;
import xing.domain.page.enums.AccessType;
import xing.infrastructure.data.CallResult;

import java.util.List;
import java.util.Map;

@Api(value = "模块相关接口")
@RestController
@RequestMapping("/module")
public class ModuleController {
    @Autowired
    ModuleService moduleService;


    @Access(accessType = AccessType.ADMIN)
    @ApiOperation(value="模块排序", notes="")
    @ApiImplicitParam(name = "arr", value = "排序数组 如：[{\"id\":123,\"sort\":2},{\"id\":124,\"sort\":3}]", required = true, dataType = "List<ModuleSort>")
    @PostMapping("/sort")
    public CallResult sort(@RequestBody List<ModuleSort> arr){

        CallResult res=moduleService.updateSort(arr);
        return res;
    }

    @Access(accessType = AccessType.ADMIN)
    @ApiOperation(value="移除模块", notes="")
    @ApiImplicitParam(name = "id", value = "模块id", required = true, dataType = "Long",paramType = "path")
    @PostMapping("/del/{corpId}/{id}")
    public CallResult del(@PathVariable Long id,@PathVariable String corpId){
        CallResult res=moduleService.deleteModule(id,corpId);
        return res;
    }

    /**
     * 描   述：新增模块
     * 创 建 人：陈天龙
     * 日    期：2017/8/9
     * @return CallResult
     */
    @Access(accessType = AccessType.ADMIN)
    @ApiOperation(value="新增模块", notes="")
    @ApiImplicitParam(name = "pageModule", value = "模块实体类", required = true, dataType = "PageModule")
    @PostMapping("/create")
    public CallResult create(@RequestBody PageModule pageModule){
        CallResult res=moduleService.createModule(pageModule);
        return res;
    }

    @ApiOperation(value="获取模块列表", notes="")
    @ApiImplicitParam(name = "para", value = "如：{\"type\":\"banner\",\"corpId\":\"123\"} 【type】模块类型 dingtalkISV：钉钉应用 banner: banner广告stationGuide：企业地图doMyself：自助应用 【corpId】企业号", required = true, dataType = "Map")
    @PostMapping("/list")
    public List<ModuleList> getModuleListSummery(@RequestBody Map<String,String> para){
        String type=para.get("type"),
                corpId=para.get("corpId");
        List<ModuleList> res=moduleService.getSummeryList(type,corpId);
        return res;
    }
    /**
    * @Description: 更换模块
    * @param:  * @param null
    * @Author: dustin
    * @Date: 2017-8-25
    */
    @Access(accessType = AccessType.ADMIN)
    @ApiOperation(value="更换模块", notes="")
    @ApiImplicitParam(name = "input", value = "", required = true, dataType = "ReplaceModuleInput")
    @PostMapping("/replace")
    public CallResult replaceModule(@RequestBody ReplaceModuleInput input){

        CallResult res=moduleService.replaceModule(input);
        return res;
    }
    /**
    * @Description: 以模块为单位，导入模块内容
    * @param:  * @param null
    * @Author: dustin
    * @Date: 2017-8-28
    */
//    @ApiOperation(value="导入模块内容", notes="")
//    @ApiImplicitParam(name = "para", value = "如：{\"sourceModuleId\":150252065223705,\"targetModuleId\":123,,\"corpId\":123}", required = true, dataType = "Map")
//    @PostMapping("/import")
//    public CallResult importModule(@RequestBody Map<String,Object> para){
//        long sourceModuleId=(Long)para.get("sourceModuleId");
//        Long targetModuleId=Long.parseLong(para.get("targetModuleId").toString());
//        String corpId=para.get("corpId").toString();
//        CallResult res=moduleService.importModuleContent(sourceModuleId,targetModuleId,corpId);
//        return res;
//    }
    @ApiOperation(value="模块详细信息", notes="")
    @ApiImplicitParam(name = "id", value = "模块id", required = true, dataType = "Long",paramType = "path")
    @PostMapping("/detail/{id}")
    public CallResult detail(@PathVariable Long id){
        CallResult res=moduleService.getModuleDetail(id);
        return res;
    }

    @ApiOperation(value="模块详细信息test", notes="")
    @ApiImplicitParam(name = "id", value = "模块id", required = true, dataType = "Long",paramType = "path")
    @PostMapping("/detail1/{id}")
    public PageModule  detail1(@PathVariable Long id){
        PageModule  res=moduleService.getModuleDetail1(id);
        return res;
    }

    @Access(accessType = AccessType.ADMIN)
    @ApiOperation(value="模块重命名", notes="")
    @ApiImplicitParam(name = "para", value = "如：{\"moduleId\":150252065223705,\"name\":\"123\"}", required = true, dataType = "Map")
    @RequestMapping(value = "/rename",method = RequestMethod.POST)
    public CallResult updateTemplateName(@RequestBody Map<String,Object> para){
        long moduleId=(Long)para.get("moduleId");
        String name=para.get("name").toString();
        CallResult res=moduleService.updateModuleName(moduleId,name);
        return res;
    }

}
